// parseCsv.js
const fs = require('fs');
const csv = require('csv-parser');
console.log('-----------------------------------------------------------------');
const results = {
    "CN": {},
    "EN": {},
    "ar": { },
    "es": {},
    "id": {},
    "jp": { },
    "kr": {},
    "pt": {},
    "th": {},
    "zh": {},
    "ug": {},
    "vi": {},
    "ru": {},
    "hid": {}
  };
const inputFilePath = 'old1/11.csv'; // 输入的 CSV 文件路径

fs.createReadStream(inputFilePath)
  .pipe(csv())
  .on('data', (data) => {    
    let jsonKey = ''
    for (const [key, value] of Object.entries(data)) {  
        //console.log('--------------- key:', key, ' value', value);  
        if(key){
          let lang = key.trim();        
            if (lang === 'key') {
                if(value){
                    let temp = value.match(/"([^"]+)"/)
                    if(temp[1]){
                        jsonKey = temp[1];
                    }
                }else{
                  console.log('---------------value:',value, ' key:',key);
                }
                
            }else{
                console.log('lang ',lang ,' jsonKey:',jsonKey, ' key:', key, ' value', value);
                results[lang][jsonKey] = value? value.trim():'';
            }
        }   
        //console.log('data list key:', key, ' value', value ,' jsonKey:', jsonKey);
    }
    console.log('results:',results);
  })
  .on('end', () => {
    // 将每一列的数据写入对应的 JSON 文件
    for (const col in results) {
      fs.writeFileSync(`old1/results.json`, JSON.stringify(results, null, 2), 'utf-8');
    }
    console.log('CSV 文件解析完成，JSON 文件已生成。');
  });